<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <!-- 品牌 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
        <a
          :class="{ active: i.selected }"
          href="javascript:;"
          v-for="i in subCates.brands"
          :key="i.id"
          @click="setBrand(i)"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <!-- 规格 -->
    <div class="item" v-for="md in subCates.saleProperties" :key="md.id">
      <div class="head">{{ md.name }}</div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
        <a
          :class="{ active: prop.selected }"
          href="javascript:;"
          v-for="prop in md.properties"
          :key="prop.id"
          @click="setProperty(prop, md.properties)"
          >{{ prop.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
import { inject } from 'vue'
export default {
  name: 'SubFilter',
  setup () {
    const subCates = inject('subCates')
    // 点击品牌高亮
    const setBrand = (brand) => {
      // 选择过就组织代码继续向下执行
      if (brand.selected) return
      console.log(brand)
      /**
       * 单选
       * 1. 清除之前选中元素的高亮 => selected=false
       * 2. 当前点击的按钮元素 => selected=true
       * 3. 避免重复点击 => 已经选过的就return
       */
      subCates.value.brands.forEach(brand => {
        brand.selected = false
      })
      brand.selected = true
    }

    // 点击规格属性高亮
    const setProperty = (prop, props) => {
      if (prop.selected) return
      console.log('当前点击规格下的所有属性数据', props)
      props.forEach(p => {
        p.selected = false
      })
      prop.selected = true
    }
    return { subCates, setBrand, setProperty }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        display: inline-block;
        margin-right: 36px;
        margin-bottom: 20px;
        transition: all 0.3s;
        border: 1px solid #eee;
        padding: 6px;
        &.active,
        &:hover {
          color: @xtxColor;
          border-color: @xtxColor;
        }
      }
    }
  }
}
</style>
